package tech.wetech.admin3.controller;

import cn.hutool.json.JSONObject;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tech.wetech.admin3.util.HashUtil;
import tech.wetech.admin3.util.LianduAPIUtils;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.Map;

@RestController
@RequestMapping("/liandu")
public class LianduAPIController {
  @Autowired
  private LianduAPIUtils lianduAPIUtil;

  @Autowired
  private HashUtil hashUtil;

  @PostMapping("/getcount")
  public JSONObject getcount(@RequestParam("reqpath") String reqpath) {
    JSONObject resp = lianduAPIUtil.getcount(reqpath);
    System.out.println("resp---" + resp);
    return resp;
  }

  @PostMapping("/sign")
  public JSONObject sign(
    @RequestParam("file") MultipartFile file,
    @RequestParam("signerCode") String signerCode,
    @RequestParam("signerName") String signerName,
    @RequestParam("signerMobile") String signerMobile,
    @RequestParam("signerType") String signerType,
    @RequestParam("page") String page,
    @RequestParam("x") String x,
    @RequestParam("y") String y
  ) throws IOException {
    JSONObject resp = lianduAPIUtil.Sign(
      file, signerCode, signerName, signerMobile, signerType,  page,  x,  y
    ).join();
    System.out.println("resp--"+resp);
    System.out.println(resp.getStr("code"));
    if (resp.getStr("code").equals("0")) {
      JSONObject data = resp.getJSONObject("data");
      String url = data.getStr("url");
      return resp;
    } else {
      return resp;
    }
  }

  @GetMapping("/getkeyexpdate")
  public JSONObject getkeyexpdate(){
    return lianduAPIUtil.getkeyexpdate();
  }

  @PostMapping("/create")
  public JSONObject create(){
    return lianduAPIUtil.create();
  }

  @PostMapping("/query")
  public JSONObject query(@RequestParam("txhash") String txhash) { return lianduAPIUtil.query(txhash);}

  // 上链存证
//  @PostMapping("/saveAuth")
//  public JSONObject saveHash(@RequestParam("image") MultipartFile image){
//    int timeStamp = (int)(System.currentTimeMillis()/1000);
//    // 计算图片哈希值
//    String dataHash = hashUtil.getFileHash(image);
//    JSONObject response = lianduAPIUtil.saveAuth(dataHash, timeStamp, "datainfo").join();
//    System.out.println("saveHash测试"+response);
//    return response;
//  }

  @PostMapping("/createDigitalHumanCredential")
  public JSONObject createDigitalHumanCredential(
    @RequestHeader("ukey") String ukey,
    @RequestBody RequestJson requestJson
  ){
    System.out.println(ukey);
    System.out.println(requestJson.did);
    System.out.println(requestJson.seconds);
    System.out.println(requestJson.data);
    return lianduAPIUtil.createDigitalHumanCredential(ukey,requestJson.did,requestJson.seconds,requestJson.data);
  }

  @PostMapping("/upload")
  public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      byte[] hashInBytes = md.digest(file.getBytes());

      // bytes to hex
      StringBuilder sb = new StringBuilder();
      for (byte b : hashInBytes) {
        sb.append(String.format("%02x", b));
      }
      String fileHash = sb.toString();

      // 这里可以进一步处理这个哈希值，比如存储、比对等操作

      return ResponseEntity.ok("Image uploaded successfully with hash value: " + fileHash);
    } catch (Exception e) {
      e.printStackTrace();
      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not upload the file: " + file.getOriginalFilename());
    }
  }


  record RequestJson(
    @NotBlank String did,
    @NotBlank int seconds,
    @NotBlank Map<String, String> data
  ){}
}
